package com.cdu.xinmiao.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.cdu.xinmiao.R;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class WeatherActivity extends AppCompatActivity {
    private TitleBar titleBar;
    private EditText etCity;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.weather), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        titleBar = findViewById(R.id.titleBar_back);
        titleBar.setLeftClickListener(v -> finish());
        etCity = findViewById(R.id.et_city);
        webView = findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 允许 JavaScript
        webView.setWebViewClient(new WebViewClient()); // 设置 WebViewClient 以在 WebView 内加载网页

    }

    public void btOk(View v) throws JSONException {
        String cityName = etCity.getText().toString().trim();
        String cityCode = getLocationIdFromExcel(cityName);
        if (!cityName.isEmpty()) {
            String url = "https://e.weather.com.cn/mweather/" + cityCode + ".shtml";
            loadWeatherPage(url);
        }
    }

    public String getLocationIdFromExcel(String location) throws JSONException {
        String cityData = "{\n" +
                "  \"101260907\": \"安龙\",\n" +
                "  \"101260908\": \"册亨\",\n" +
                "  \"101260909\": \"普安\",\n" +
                "  \"101270101\": \"成都\",\n" +
                "  \"101270102\": \"龙泉驿\",\n" +
                "  \"101270103\": \"新都\",\n" +
                "  \"101270104\": \"温江\",\n" +
                "  \"101270105\": \"金堂\",\n" +
                "  \"101270106\": \"双流\",\n" +
                "  \"101270107\": \"郫都\",\n" +
                "  \"101270108\": \"大邑\",\n" +
                "  \"101270109\": \"蒲江\",\n" +
                "  \"101270110\": \"新津\",\n" +
                "  \"101270111\": \"都江堰\",\n" +
                "  \"101270112\": \"彭州\",\n" +
                "  \"101270113\": \"邛崃\",\n" +
                "  \"101270114\": \"崇州\",\n" +
                "  \"101270115\": \"青白江\",\n" +
                "  \"101270116\": \"锦江\",\n" +
                "  \"101270117\": \"青羊\",\n" +
                "  \"101270118\": \"金牛\",\n" +
                "  \"101270119\": \"武侯\",\n" +
                "  \"101270120\": \"成华\"\n" +
                "}";
        JSONObject jsonData = new JSONObject(cityData);

        String cityId = null;
        Iterator<String> keys = jsonData.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            try {
                if (jsonData.getString(key).equals(location)) {
                    cityId = key;
                    break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return cityId;
    }


    private void loadWeatherPage(String url) {
        webView.loadUrl(url);
    }
}
